from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class OLOption:

    def __init__(self, driver):
        self.driver = driver

    def click_to_financial(self):
        WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(
            (By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/a')
        )).click()

    def click_to_ol(self):
        ele = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(
            (By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/ul/li[2]/a')
        ))
        ele.click()

    def apply_money(self, money, deadline):
        WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(
            (By.XPATH, '//*[@id="amount"]')
        )).send_keys(money)

        WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(
            (By.XPATH, '//*[@id="term"]')
        )).send_keys(deadline)

        WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(
            (By.XPATH, '//*[@id="submit"]')
        )).click()

    def confirm_apply(self, flag=False):
        xpath = '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]' if (
            flag) else '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[2]'
        WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(
            (By.XPATH, xpath)
        )).click()

    def get_result_tip(self):
        return WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(
            (By.CSS_SELECTOR, '.jconfirm-content div')
        )).text

    def click_to_online_lending(self):
        WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(
            (By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[5]/a')
        )).click()

    def click_to_check_ol(self):
        WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(
            (By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[5]/ul/li[1]/a')
        )).click()

    def click_pass(self):
        WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(
            (By.XPATH, '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr/td[7]/button[1]')
        )).click()

    def click_confirm(self):
        WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(
            (By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]')
        )).click()
